package com.zy.fee.roadpark.receiver;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.zy.fee.roadpark.activity.MainActivity;
import java.util.List;

/**
 * Created by Wikison on 2019/3/28.
 */
public class BootReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(final Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
      try {
        if (checkAppActivityState(context, "com.zy.fee.roadpark") < 0) {
          Intent newIntent = new Intent(context, MainActivity.class);
          newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(newIntent);
        }

      } catch (Exception e) {
      }
    } else {

    }
  }

  private int checkAppActivityState(Context context, String sPackage) {
    int iResult = -1;
    int i = -1;
    try {
      ActivityManager actTemp = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
      List<ActivityManager.RunningTaskInfo> runningTasks = actTemp.getRunningTasks(100);
      for (ActivityManager.RunningTaskInfo taskInfo : runningTasks) {
        i++;
        if (taskInfo.baseActivity.getPackageName().equals(sPackage)) {
          iResult = i;
          break;
        }
      }
    } catch (Exception e) {
    }
    return iResult;
  }
}
